﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XChipTool.Enties;
using System.IO.Ports;
using System.Configuration;
using SerialPortHelperLib;
using XChip_Common;
using BinAnalysisHelp;
using AdbService;

namespace XChipTool
{
    public partial class XChip_HDR_Ae : DevExpress.XtraEditors.XtraUserControl
    {
        public SerialPort sPortTemp;
        private bool isRead = false;
        private KeyEventArgs keyEnter = new KeyEventArgs(Keys.Enter);//用来手动触发文本框下发配置
        public addressregisterManager aBll;
        private List<addressregister> xChipHDRAe;
        public bool IsUsbOpen = false;
        //记录是否使用I2C设备读写
        public bool m_open = false;

        // 使用服务定位器获取ADB服务
        private IAdbCommandService adbService = ServiceLocator.AdbCommandService;
        public List<addressregister> XChipHDRAe
        {
            get
            {
                if (null == xChipHDRAe)
                {
                    xChipHDRAe = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "HDR_AE" || c.Classify == "Common"));
                }
                return xChipHDRAe;
            }

            set
            {
                xChipHDRAe = value;
            }
        }

        public XChip_HDR_Ae()
        {
            //if (null == xChipHDRAe)
            //{
            //       //aBll = new addressregisterManager();
            //       //xChipHDRAe = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "HDR_AE" || c.Classify == "Common"));
            //}
            InitializeComponent();
        }
        public XChip_HDR_Ae(SerialPort _sPortTemp, addressregisterManager _aBll)
        {
            sPortTemp = _sPortTemp;
            aBll = _aBll;
           
            InitializeComponent();
        }
        private async void btn_Refresh_Click(object sender, EventArgs e)
        {
            if (AE_WEIGHT_EN.Checked)
            {
                await WriteRegisterAsync(0x20000077.ToString("X"),0x01.ToString("X"));
            }
        }

        private Control[] Controls_Find(string str)
        {
            //var t =   this.Controls.Find(type, true);
            List<Control> listc = new List<Control>();
            System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
           
            for (int i = 0; i < fieldInfo.Length; i++)
            {
                if (fieldInfo[i].Name.ToLower().Contains(str.ToLower())&& !listc.Exists(c=>c.Name.ToLower()==(fieldInfo[i].Name.ToLower())))
                {
                    listc.AddRange(this.Controls.Find(fieldInfo[i].Name, true));
                    //break;
                }
            }
            return listc.ToArray();
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void simpleButton5_Click(object sender, EventArgs e)
        {
            try
            {
                isRead = true;
                WaitBefore.Start("读取中", "正在加载中", 10);
                List<string> NameList = new List<string>();
                List<string> listfiltration = new List<string> { "L_num", "DR" };

                for (int i = 0; i < xChipHDRAe.Count; i++)
                {
                    
                    var type = AppHelper.NameTypeToString(xChipHDRAe[i].NameType);
                    if (NameList.Exists(c => c.ToLower() == (type)))
                    {
                        continue;
                    }
                    
                    var col = Controls_Find(type);
                    foreach (var item in col)
                    {
                        if (item is TextEdit textEdit)
                        {
                            var nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            if (item.Name.ToLower() == type.ToLower())
                            {
                                nList = xChipHDRAe.Where(c => c.NameType.ToLower() == (item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            }

                            if (nList.Count == 0)
                            {
                                nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            }
                            if (nList.Count == 1)
                            {
                                // 单个寄存器读取
                               await ReadRegisterAndUpdateTextAsync(nList[0].Address, textEdit, nList[0].Bits, nList[0].Hex);
                                
                            }
                            else
                            {
                                // 多个寄存器组合读取
                                await ReadMultipleRegistersAndUpdateTextAsync(nList, textEdit);
                                
                            }
                        }
                        else if (item is CheckEdit checkEdit)
                        {
                            var nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                            if (nList.Count == 1)
                            {
                                await  ReadRegisterAndUpdateCheckAsync(nList[0].Address, checkEdit, nList[0].Bits, nList[0].Hex);
                                
                            }
                        }
                    }
                }

               

                // 其他读取操作...
                var nValue = await ReadRegisterAsync(xChipHDRAe.Find(c => c.NameType.ToLower().Contains("Flicker_Long".ToLower()) && c.ControlType == "0").Address);
                // ... 更新Flicker_Long ...

                var speedModeValue = await ReadRegisterAsync(xChipHDRAe.Find(c => c.NameType.ToLower().Contains("HDRAE_SpeedModeradio".ToLower()) && c.ControlType == "0").Address);
                // ... 更新HDRAE_SpeedModeradio ...
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("出现错误信息：" + ex.ToString());
            }
            finally
            {
                isRead = false;
                WaitBefore.Close();
            }
        }

        private async void simpleButton3_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsUsbOpen || m_open)
                {
                    // ADB模式，不检查串口
                }
                else
                {
                    if (!sPortTemp.IsOpen)
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                        return;
                    }
                }

                List<string> listfiltration = new List<string> { "Expo_Long", "Expo_Short", "Expo_VeryShort", "GainReadOnly", "TargetY_L", "TargetY_S" };

                isRead = true;
                WaitBefore.Start("读取中", "正在加载中", 10);

                
                List<string> NameList = new List<string>();

                for (int i = 0; i < xChipHDRAe.Count; i++)
                {
                    if (xChipHDRAe[i].NameType.Contains("Expo_L"))
                    {
                        // 空处理
                    }

                    var type = AppHelper.NameTypeToString(xChipHDRAe[i].NameType);
                    if (NameList.Exists(c => c.ToLower() == (type)))
                    {
                        continue;
                    }

                    var col = Controls_Find(type);

                    foreach (var item in col)
                    {
                        if (!listfiltration.Exists(c => c.Contains(item.Name.ToString())))
                        {
                            continue;
                        }

                        if (item is TextEdit textEdit)
                        {
                            var nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();

                            if (item.Name.ToLower() == type.ToLower())
                            {
                                nList = xChipHDRAe.Where(c => c.NameType.ToLower() == (item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            }

                            if (nList.Count == 0)
                            {
                                nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            }

                            if (nList.Count == 1)
                            {
                                // 单个寄存器读取
                                await ReadRegisterAndUpdateTextAsync(nList[0].Address, textEdit, nList[0].Bits, nList[0].Hex);
                               
                            }
                            else
                            {
                                // 多个寄存器组合读取
                                await ReadMultipleRegistersAndUpdateTextAsync(nList, textEdit);
                                
                            }
                        }
                        else if (item is CheckEdit checkEdit)
                        {
                            var nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                            if (nList.Count == 1)
                            {
                                // 复选框读取
                                await  ReadRegisterAndUpdateCheckAsync(nList[0].Address, checkEdit, nList[0].Bits, nList[0].Hex);
                                
                            }
                        }
                        NameList.Add(item.Name);
                    }
                }

                
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("出现错误信息：" + ex.ToString());
            }
            finally
            {
                isRead = false;
                WaitBefore.Close();
            }
        }



        /// <summary>
        /// 加入提示信息和正则判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XChip_HDR_Ae_Load(object sender, EventArgs e)
        {
            List<string> NameList = new List<string>();
            for (int i = 0; i < XChipHDRAe.Count; i++)
            {
              
                var type = AppHelper.NameTypeToString(XChipHDRAe[i].NameType);
                if (NameList.Exists(c => c.ToLower() == (type)))
                {
                    continue;
                }
                var col = Controls_Find(type);
                foreach (var item in col)
                {
                    if (item is TextEdit)
                    {
                        //声明一个SuerToolTip
                        DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                        //声明一个ToolTipTitleItem,保存标题
                        DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                        String tooltips = "";

                        if (!string.IsNullOrEmpty(XChipHDRAe[i].Bit_Wide))
                        {
                            tooltips += $"位宽={XChipHDRAe[i].Bit_Wide}";
                        }
                        if (!string.IsNullOrEmpty(XChipHDRAe[i].Bit_Accuracy))
                        {
                            tooltips += $"|精度={XChipHDRAe[i].Bit_Accuracy}";
                        }
                        //设置标题文字
                        toolTipTitleItem.Text = tooltips;
                        //把标题添加到SuerToolTip
                        graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                        //指定控件的SuperTip

                        ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                        //正则判断
                        ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                        ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipHDRAe[i].Bits, XChipHDRAe[i].Bit_Wide, XChipHDRAe.Where(c => c.NameType.ToLower().Contains(type) && c.ControlType == "0").Select(c => c.Bits).ToList());

                    }
                    NameList.Add(item.Name);

                }
            }
        }

        private void Area_Weight_01_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Area_Weight_01_Leave(sender, null);
            }
        }
        private async Task SetHDRAeRegister(addressregister model, ulong add, bool isCheck = true)
        {

            if (isRead) return;

            bool success = await WriteRegisterAsync(model.Address, add.ToString("X"));

            if (!success)
            {
                Console.WriteLine($"[ADBForm] SetCfaRegister 写入失败 - 地址: {model.Address}, 值: {add:X}");
            }

           
        }
       
        private void Area_Weight_01_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void adaptive_dr_table_01_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                adaptive_dr_table_01_Leave(sender, null);
            }
        }

        private void adaptive_dr_table_01_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void L_number_adaptive_01_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                L_number_adaptive_01_Leave(sender, null);
            }
        }

        private void L_number_adaptive_01_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void TargetY_L_adaptive_01_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TargetY_L_adaptive_01_Leave(sender, null);
            }
        }

        private void TargetY_L_adaptive_01_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void TargetY_S_adaptive_01_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TargetY_S_adaptive_04_Leave(sender, null);
            }
        }

        private void TargetY_S_adaptive_04_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void Max_Expo_L_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Max_Expo_L_Leave(sender, null);
            }
        }

        private void Max_Expo_L_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void Max_Gain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Max_Gain_Leave(sender, null);
            }
        }

        private void Max_Gain_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void Max_Expo_S_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Max_Expo_S_Leave(sender, null);
            }
        }

        private void Max_Expo_S_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void Max_Expo_VS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Max_Expo_VS_Leave(sender, null);
            }
        }

        private void Max_Expo_VS_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void trigger_timer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                trigger_timer_Leave(sender, null);
            }
        }

        private void trigger_timer_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void dr_wave_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                dr_wave_Leave(sender, null);
            }
        }

        private void dr_wave_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void L_num_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                L_num_Leave(sender, null);
            }
        }

        private void L_num_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void TargetY_L_ex_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TargetY_L_ex_Leave(sender, null);
            }
        }

        private void TargetY_L_ex_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void TargetY_S_ex_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TargetY_S_ex_Leave(sender, null);
            }
        }

        private void TargetY_S_ex_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void S_num_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                S_num_Leave(sender, null);
            }
        }

        private void S_num_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void Flicker_50Hz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Flicker_50Hz_Leave(sender, null);
            }
        }

        private void Flicker_50Hz_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void Flicker_60Hz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Flicker_60Hz_Leave(sender, null);
            }
        }

        private void Flicker_60Hz_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void Flicker_Long_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEventArgs e)
        {
                CheckedListBoxControl chkControl = sender as CheckedListBoxControl;
               SingleSelectCheckedListBoxControls(chkControl, e.Index);
        }
        public void SingleSelectCheckedListBoxControls(CheckedListBoxControl chkControl, int index)
        {
            if (chkControl.CheckedItems.Count > 0)
            {
                for (int i = 0; i < chkControl.Items.Count; i++)
                {
                    if (i != index)
                    {
                        chkControl.SetItemCheckState(i, System.Windows.Forms.CheckState.Unchecked);
                    }
                }
            }
        }

        private void Flicker_Short_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEventArgs e)
        {
            CheckedListBoxControl chkControl = sender as CheckedListBoxControl;
            SingleSelectCheckedListBoxControls(chkControl, e.Index);
        }

        private void Flicker_Very_Short_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEventArgs e)
        {
            CheckedListBoxControl chkControl = sender as CheckedListBoxControl;
            SingleSelectCheckedListBoxControls(chkControl, e.Index);
        }

        private void Flicker_Long_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as CheckedListBoxControl;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;

            //if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            //{
            //    var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
            //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.SelectedValue.ToString());
            //    SetHDRAeRegister(model, Val);
            //}

            //if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                if (txtEdit.Items[0].CheckState == CheckState.Checked)//50hz
                {
                    var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                    SetHDRAeRegister(model, (ulong)0x02);
                }
                if (txtEdit.Items[1].CheckState == CheckState.Checked)//60hz
                {
                    var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                    SetHDRAeRegister(model, (ulong)0x01);
                }
                if ((txtEdit.Items[1].CheckState == CheckState.Unchecked) && (txtEdit.Items[0].CheckState == CheckState.Unchecked))//均不勾选
                {
                    var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                    SetHDRAeRegister(model, (ulong)0x00);
                }
            }
            EnShowHide();

        }

        private void Flicker_Short_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as CheckedListBoxControl;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.SelectedValue.ToString());
                SetHDRAeRegister(model, Val);
            }
        }

        private void Flicker_Very_Short_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as CheckedListBoxControl;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.SelectedValue.ToString());
                SetHDRAeRegister(model, Val);
            }
        }

        private void HDRAE_SpeedModeradio_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as RadioGroup;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Properties.Items[txtEdit.SelectedIndex].Value.ToString());
                SetHDRAeRegister(model, Val);
            }
        }

        private void slow_b2d_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                slow_b2d_Leave(sender, null);
            }
        }

        private void slow_b2d_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private async void speed_mode_man_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), false);//强制写0
               await SetHDRAeRegister(model, Val);
            }
            if (chk.Checked)
            {
                if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{HDRAE_SpeedModeradio.Name}".ToLower()))
                {
                    var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{HDRAE_SpeedModeradio.Name}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, "1".ToString());
                    SetHDRAeRegister(model, Val);
                }
            }
            else
            {
                if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{HDRAE_SpeedModeradio.Name}".ToLower()))
                {
                    var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{HDRAE_SpeedModeradio.Name}".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, "0".ToString());
                    SetHDRAeRegister(model, Val);
                }
            }
            EnShowHide();
        }

        private async void trigger_timer_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
                await SetHDRAeRegister(model, Val);
            }
        }

        private async void adaptive_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
                await SetHDRAeRegister(model, Val);
            }
            Statusvalue();
        }

        private void Statusvalue()
        {
            if (adaptiveTotal_en.Checked)
            {
                L_number_adaptive_en.Enabled = true;
                TargetY_L_adaptive_en.Enabled = true;
                TargetY_S_adaptive_en.Enabled = true;

                adaptive_dr_table_01.Enabled = true;
                adaptive_dr_table_02.Enabled = true;
                adaptive_dr_table_03.Enabled = true;
                adaptive_dr_table_04.Enabled = true;
                adaptive_dr_table_05.Enabled = true;
                adaptive_dr_table_06.Enabled = true;
                adaptive_dr_table_07.Enabled = true;
                adaptive_dr_table_08.Enabled = true;

                L_number_adaptive_01.Enabled = true;
                L_number_adaptive_02.Enabled = true;
                L_number_adaptive_03.Enabled = true;
                L_number_adaptive_04.Enabled = true;
                L_number_adaptive_05.Enabled = true;
                L_number_adaptive_06.Enabled = true;
                L_number_adaptive_07.Enabled = true;
                L_number_adaptive_08.Enabled = true;

                TargetY_L_adaptive_01.Enabled = true;
                TargetY_L_adaptive_02.Enabled = true;
                TargetY_L_adaptive_03.Enabled = true;
                TargetY_L_adaptive_04.Enabled = true;
                TargetY_L_adaptive_05.Enabled = true;
                TargetY_L_adaptive_06.Enabled = true;
                TargetY_L_adaptive_07.Enabled = true;
                TargetY_L_adaptive_08.Enabled = true;

                TargetY_S_adaptive_01.Enabled = true;
                TargetY_S_adaptive_02.Enabled = true;
                TargetY_S_adaptive_03.Enabled = true;
                TargetY_S_adaptive_04.Enabled = true;
                TargetY_S_adaptive_05.Enabled = true;
                TargetY_S_adaptive_06.Enabled = true;
                TargetY_S_adaptive_07.Enabled = true;
                TargetY_S_adaptive_08.Enabled = true;
                if (L_number_adaptive_en.Checked)
                {
                    //adaptive_dr_table_01.Enabled = true;
                    //adaptive_dr_table_02.Enabled = true;
                    //adaptive_dr_table_03.Enabled = true;
                    //adaptive_dr_table_04.Enabled = true;
                    //adaptive_dr_table_05.Enabled = true;
                    //adaptive_dr_table_06.Enabled = true;
                    //adaptive_dr_table_07.Enabled = true;
                    //adaptive_dr_table_08.Enabled = true;

                    L_number_adaptive_01.Enabled = true;
                    L_number_adaptive_02.Enabled = true;
                    L_number_adaptive_03.Enabled = true;
                    L_number_adaptive_04.Enabled = true;
                    L_number_adaptive_05.Enabled = true;
                    L_number_adaptive_06.Enabled = true;
                    L_number_adaptive_07.Enabled = true;
                    L_number_adaptive_08.Enabled = true;

                    L_num.Enabled = false;
                }
                else
                {
                    //adaptive_dr_table_01.Enabled = false;
                    //adaptive_dr_table_02.Enabled = false;
                    //adaptive_dr_table_03.Enabled = false;
                    //adaptive_dr_table_04.Enabled = false;
                    //adaptive_dr_table_05.Enabled = false;
                    //adaptive_dr_table_06.Enabled = false;
                    //adaptive_dr_table_07.Enabled = false;
                    //adaptive_dr_table_08.Enabled = false;

                    L_number_adaptive_01.Enabled = false;
                    L_number_adaptive_02.Enabled = false;
                    L_number_adaptive_03.Enabled = false;
                    L_number_adaptive_04.Enabled = false;
                    L_number_adaptive_05.Enabled = false;
                    L_number_adaptive_06.Enabled = false;
                    L_number_adaptive_07.Enabled = false;
                    L_number_adaptive_08.Enabled = false;

                    L_num.Enabled = true;
                }
                if (TargetY_L_adaptive_en.Checked)
                {
                    TargetY_L_adaptive_01.Enabled = true;
                    TargetY_L_adaptive_02.Enabled = true;
                    TargetY_L_adaptive_03.Enabled = true;
                    TargetY_L_adaptive_04.Enabled = true;
                    TargetY_L_adaptive_05.Enabled = true;
                    TargetY_L_adaptive_06.Enabled = true;
                    TargetY_L_adaptive_07.Enabled = true;
                    TargetY_L_adaptive_08.Enabled = true;

                    TargetY_L_ex.Enabled = false;
                }
                else
                {
                    TargetY_L_adaptive_01.Enabled = false;
                    TargetY_L_adaptive_02.Enabled = false;
                    TargetY_L_adaptive_03.Enabled = false;
                    TargetY_L_adaptive_04.Enabled = false;
                    TargetY_L_adaptive_05.Enabled = false;
                    TargetY_L_adaptive_06.Enabled = false;
                    TargetY_L_adaptive_07.Enabled = false;
                    TargetY_L_adaptive_08.Enabled = false;


                    TargetY_L_ex.Enabled = true;
                }
                if (TargetY_S_adaptive_en.Checked)
                {
                    TargetY_S_adaptive_01.Enabled = true;
                    TargetY_S_adaptive_02.Enabled = true;
                    TargetY_S_adaptive_03.Enabled = true;
                    TargetY_S_adaptive_04.Enabled = true;
                    TargetY_S_adaptive_05.Enabled = true;
                    TargetY_S_adaptive_06.Enabled = true;
                    TargetY_S_adaptive_07.Enabled = true;
                    TargetY_S_adaptive_08.Enabled = true;

                    TargetY_S_ex.Enabled = false;
                }
                else
                {

                    TargetY_S_adaptive_01.Enabled = false;
                    TargetY_S_adaptive_02.Enabled = false;
                    TargetY_S_adaptive_03.Enabled = false;
                    TargetY_S_adaptive_04.Enabled = false;
                    TargetY_S_adaptive_05.Enabled = false;
                    TargetY_S_adaptive_06.Enabled = false;
                    TargetY_S_adaptive_07.Enabled = false;
                    TargetY_S_adaptive_08.Enabled = false;


                    TargetY_S_ex.Enabled = true;
                }
            }
            else
            {
                L_number_adaptive_en.Enabled = false;
                TargetY_L_adaptive_en.Enabled = false;
                TargetY_S_adaptive_en.Enabled = false;

                adaptive_dr_table_01.Enabled = false;
                adaptive_dr_table_02.Enabled = false;
                adaptive_dr_table_03.Enabled = false;
                adaptive_dr_table_04.Enabled = false;
                adaptive_dr_table_05.Enabled = false;
                adaptive_dr_table_06.Enabled = false;
                adaptive_dr_table_07.Enabled = false;
                adaptive_dr_table_08.Enabled = false;

                L_number_adaptive_01.Enabled = false;
                L_number_adaptive_02.Enabled = false;
                L_number_adaptive_03.Enabled = false;
                L_number_adaptive_04.Enabled = false;
                L_number_adaptive_05.Enabled = false;
                L_number_adaptive_06.Enabled = false;
                L_number_adaptive_07.Enabled = false;
                L_number_adaptive_08.Enabled = false;

                TargetY_L_adaptive_01.Enabled = false;
                TargetY_L_adaptive_02.Enabled = false;
                TargetY_L_adaptive_03.Enabled = false;
                TargetY_L_adaptive_04.Enabled = false;
                TargetY_L_adaptive_05.Enabled = false;
                TargetY_L_adaptive_06.Enabled = false;
                TargetY_L_adaptive_07.Enabled = false;
                TargetY_L_adaptive_08.Enabled = false;

                TargetY_S_adaptive_01.Enabled = false;
                TargetY_S_adaptive_02.Enabled = false;
                TargetY_S_adaptive_03.Enabled = false;
                TargetY_S_adaptive_04.Enabled = false;
                TargetY_S_adaptive_05.Enabled = false;
                TargetY_S_adaptive_06.Enabled = false;
                TargetY_S_adaptive_07.Enabled = false;
                TargetY_S_adaptive_08.Enabled = false;

                L_num.Enabled = true;
                TargetY_L_ex.Enabled = true;
                TargetY_S_ex.Enabled = true;
            }
            
        }

        private async void L_number_adaptive_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
                await SetHDRAeRegister(model, Val);
            }
            Statusvalue();
        }

        private async void TargetY_L_adaptive_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
                await SetHDRAeRegister(model, Val);
            }
            Statusvalue();
        }

        private async void TargetY_S_adaptive_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
                await SetHDRAeRegister(model, Val);
            }
            Statusvalue();
        }

        private void AE_WEIGHT_EN_CheckedChanged(object sender, EventArgs e)
        {
            if (AE_WEIGHT_EN.Checked)
            {

                for (int i = 0; i < 64; i++)
                {
                    foreach (Control item in panelControl1.Controls)
                    {
                        if ($"Area_Weight_{(i + 1).ToString("D2")}" == item.Name)
                        {
                            item.Enabled = true;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < 64; i++)
                {
                    foreach (Control item in panelControl1.Controls)
                    {
                        if ($"Area_Weight_{(i + 1).ToString("D2")}" == item.Name)
                        {
                            item.Enabled = false;
                        }
                    }
                }
            }
        }

        private async void Isp_Gain_en_CheckedChanged(object sender, EventArgs e)
        {
            var chk = sender as CheckEdit;
            if (isRead) return;
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == (chk.Name).ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == (chk.Name).ToLower());
                var Add = await ReadRegisterAsync(model.Address);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), chk.Checked);
              await  SetHDRAeRegister(model, Val);
            }
        }

        private void factor_Leave(object sender, EventArgs e)
        {
            if (isRead) return;
            var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            if (string.IsNullOrEmpty(txtEdit.Text)) return;

            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m1".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_m0".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
            if (XChipHDRAe.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
            {
                var model = XChipHDRAe.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                SetHDRAeRegister(model, Val);
            }
        }

        private void factor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                factor_Leave(sender, null);
            }
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {

           
            
            List<string> listfiltration = new List<string> { "L_num", "DR", "TargetY_L_ex", "TargetY_S_ex" };

            isRead = true;
            WaitBefore.Start("读取中", "正在加载中", 10);
            List<string> NameList = new List<string>();

            for (int i = 0; i < xChipHDRAe.Count; i++)
            {
                if (xChipHDRAe[i].NameType.Contains("TargetY_L"))
                {

                }
                var type = AppHelper.NameTypeToString(xChipHDRAe[i].NameType);
                if (NameList.Exists(c => c.ToLower() == (type)))
                {
                    continue;
                }

                ////加上单选框的_en
                //if (xChipHDRAe[i].ControlType == "1")
                //{
                //    type = xChipHDRAe[i].NameType;
                //}
                var col = Controls_Find(type);

                foreach (var item in col)
                {
                    if (!listfiltration.Any(itemss => item.Name==(itemss)))
                    {
                        continue;
                    }
                    if (item is TextEdit textEdit)
                    {
                        var nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();

                        if (item.Name.ToLower() == type.ToLower())
                        {
                            nList = xChipHDRAe.Where(c => c.NameType.ToLower() == (item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        }

                        if (nList.Count == 0)
                        {
                            nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        }

                        if (nList.Count == 1)
                        {
                            // 单个寄存器读取
                            await ReadRegisterAndUpdateTextAsync(nList[0].Address, textEdit, nList[0].Bits, nList[0].Hex);

                        }
                        else
                        {
                            // 多个寄存器组合读取
                            await ReadMultipleRegistersAndUpdateTextAsync(nList, textEdit);

                        }
                    }
                    else if (item is CheckEdit checkEdit)
                    {
                        var nList = xChipHDRAe.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                        if (nList.Count == 1)
                        {
                            // 复选框读取
                            await ReadRegisterAndUpdateCheckAsync(nList[0].Address, checkEdit, nList[0].Bits, nList[0].Hex);

                        }
                    }
                    NameList.Add(item.Name);

                }

                //NameList.Add(type);
            }
            isRead = false;
            WaitBefore.Close();
        }


        private void simpleButton6_Click(object sender, EventArgs e)
        {

        }
        private void EnShowHide()
        {
            if (Flicker_Long.Items[0].CheckState == CheckState.Checked)//50hz
            {
                Flicker_50Hz.Enabled = true;
                Flicker_60Hz.Enabled = false;
            }
            if (Flicker_Long.Items[1].CheckState == CheckState.Checked)//60hz
            {
                Flicker_50Hz.Enabled = false;
                Flicker_60Hz.Enabled = true;
            }
            if ((Flicker_Long.Items[1].CheckState == CheckState.Unchecked) && (Flicker_Long.Items[0].CheckState == CheckState.Unchecked))//均不勾选
            {
                Flicker_50Hz.Enabled = false;
                Flicker_60Hz.Enabled = false;
            }

            slow_b2d.Enabled = speed_mode_man_en.Checked;
            slow_d2b.Enabled = speed_mode_man_en.Checked;
        }


        #region ADB异步读写

        /// <summary>
        /// 异步读取单个寄存器并更新文本框
        /// </summary>
        private async Task ReadRegisterAndUpdateTextAsync(string address, TextEdit textEdit, string bits, string hex)
        {
            try
            {
                var value = await ReadRegisterAsync(address);
                string val = value.ToString("X");
                textEdit.Text = BitsHelp.GetBitsToUint(bits, hex, val).ToString("X");
            }
            catch (Exception ex)
            {
                textEdit.Text = "Error";
            }
        }

        /// <summary>
        /// 异步读取多个寄存器并组合更新文本框
        /// </summary>
        private async Task ReadMultipleRegistersAndUpdateTextAsync(List<addressregister> nList, TextEdit textEdit)
        {
            try
            {
                string val = string.Empty;

                // 多个寄存器组合读取
                if (nList.Exists(c => c.NameType.ToLower().Contains("_h")))
                {
                    val += (await ReadRegisterAsync(nList.Find(c => c.NameType.ToLower().Contains("_h")).Address)).ToString("X2");
                }
                if (nList.Exists(c => c.NameType.ToLower().Contains("_m1")))
                {
                    val += (await ReadRegisterAsync(nList.Find(c => c.NameType.ToLower().Contains("_m1")).Address)).ToString("X2");
                }
                if (nList.Exists(c => c.NameType.ToLower().Contains("_m0")))
                {
                    val += (await ReadRegisterAsync(nList.Find(c => c.NameType.ToLower().Contains("_m0")).Address)).ToString("X2");
                }
                if (nList.Exists(c => c.NameType.ToLower().Contains("_l")))
                {
                    if (nList.Exists(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")))
                    {
                        val += (await ReadRegisterAsync(nList.Find(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")).Address)).ToString("X2");
                    }
                    else
                    {
                        val += (await ReadRegisterAsync(nList.Last(c => c.NameType.ToLower().Contains("_l")).Address)).ToString("X2");
                    }
                }

                textEdit.Text = val;
            }
            catch (Exception ex)
            {
                textEdit.Text = "Error";
            }
        }

        /// <summary>
        /// 异步读取寄存器并更新复选框
        /// </summary>
        private async Task ReadRegisterAndUpdateCheckAsync(string address, CheckEdit checkEdit, string bits, string hex)
        {
            try
            {
                var value = await ReadRegisterAsync(address);
                string val = value.ToString("X");
                checkEdit.Checked = BitsHelp.GetBitsCheckedToBool(bits, hex, val) == 0;
            }
            catch (Exception ex)
            {
                checkEdit.CheckState = CheckState.Unchecked;
            }
        }

        /// <summary>
        /// 异步读取单个寄存器返回值
        /// </summary>
        private async Task<long> ReadRegisterAsync(string address)
        {
            try
            {
                // 发送读取命令
                string adbCommand = $"adb shell setprop vendor.extcamera.xxw.ispregctrl \"0,{address},0,0\"";
                await adbService.ExecuteAdbCommandAsync(adbCommand, $"读操作 - 地址:{address}");

                // 等待命令执行
                await Task.Delay(10);

                // 获取寄存器值
                var record = await Task.Run(() => LogcatDataManager.FindByRegisterAddressSync(address, "Read", 500));

                if (record != null && !string.IsNullOrEmpty(record.Values) && record.Values != "0")
                {
                    return Convert.ToInt32(record.Values, 16);
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// 异步写入单个寄存器
        /// </summary>
        private async Task<bool> WriteRegisterAsync(string address, string value)
        {
            try
            {
                // 处理位操作（如果有）
                string processedValue = value;

                // 确保值是十六进制格式（不带0x前缀）
                if (processedValue.StartsWith("0x"))
                {
                    processedValue = processedValue.Substring(2);
                }

                // 发送写入命令
                string adbCommand = $"adb shell setprop vendor.extcamera.xxw.ispregctrl \"1,{address},{processedValue},0\"";
                await adbService.ExecuteAdbCommandAsync(adbCommand, $"写操作 - 地址:{address} 值:{processedValue}");

                // 等待命令执行
                await Task.Delay(10);

                // 验证写入是否成功 - 等待写入记录出现
                var writeRecord = await Task.Run(() => LogcatDataManager.FindByRegisterAddressSync(address, "Write", 500));

                if (writeRecord != null && !string.IsNullOrEmpty(writeRecord.Values))
                {
                    string actualValue = writeRecord.Values;
                    if (actualValue.StartsWith("0x"))
                    {
                        actualValue = actualValue.Substring(2);
                    }

                    // 比较写入的值是否匹配
                    return string.Equals(actualValue, processedValue, StringComparison.OrdinalIgnoreCase);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #endregion

    }
}
